//
//  NSDate+Extension.swift
//  SinaWeibo
//
//  Created by 郭平 on 15/10/18.
//  Copyright © 2015年 郭平. All rights reserved.
//

import Foundation

extension NSDate {
    
    /**
    判断是否是今年
    
    - parameter target: 获取传入的年份
    
    - returns: 返回是或否
    */
    func isThisYear(target: NSDate) -> Bool {
    
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy"
        
        let targetStr = formatter.stringFromDate(target)
        let currentStr = formatter.stringFromDate(self)
        
        return (targetStr as NSString).isEqualToString(currentStr)
    }
    
    /**
    判断是否是今天
    
    - parameter target: 获取传入的年月日
    
    - returns: <#return value description#>
    */
    func isToday(target: NSDate) -> Bool {
       
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        
        let targetStr = formatter.stringFromDate(target)
        let currentStr = formatter.stringFromDate(self)
        
        return (targetStr as NSString).isEqualToString(currentStr)
    }
    
    /**
    判断是否是昨天
    
    - parameter target: <#target description#>
    
    - returns: <#return value description#>
    */
    func isYesterday(var target: NSDate) -> Bool {
    
        var currentDate = NSDate()
        
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        
        let targetStr = formatter.stringFromDate(target)
        let currentStr = formatter.stringFromDate(currentDate)
        
        target = formatter.dateFromString(targetStr)!
        currentDate = formatter.dateFromString(currentStr)!
        
        // 求出两个时间之间的间隔(单位:秒)
        let result = currentDate.timeIntervalSinceDate(target)
        
        if result == 24 * 60 * 60 {
            return true
        }
        return false
    }
    
}